package com.tree.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tree.backend.entity.User;
import com.tree.backend.exception.GlobalException;
import com.tree.backend.mapper.UserMapper;
import com.tree.backend.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tree.backend.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author tree
 * @since 2025-10-24
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public String login(User user) {
        LambdaQueryWrapper<User> eqName = new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername());
        User one = this.getOne(eqName);
        if (one == null) throw new GlobalException("用户名不存在");
        if (!one.getPassword().equals(user.getPassword())) throw new GlobalException("密码错误");
        // 生成Token
        JwtUtil jwtUtil = new JwtUtil();
        return jwtUtil.generateToken(one.getUsername());
    }

    @Override
    public IPage<User> getUserList(Page<User> page, User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        // 如果有用户名查询条件
        if (user.getUsername() != null && !user.getUsername().isEmpty()) {
            queryWrapper.like(User::getUsername, user.getUsername());
        }
        // 按创建时间倒序排列
        queryWrapper.orderByDesc(User::getCreateTime);
        return this.page(page, queryWrapper);
    }

    @Override
    public User getUserById(Integer id) {
        User user = this.getById(id);
        if (user == null) {
            throw new GlobalException("用户不存在");
        }
        return user;
    }

    @Override
    public boolean addUser(User user) {
        // 检查用户名是否已存在
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        if (this.count(queryWrapper) > 0) {
            throw new GlobalException("用户名已存在");
        }
        
        // 设置创建和更新时间
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        
        return this.save(user);
    }

    @Override
    public boolean updateUser(User user) {
        // 检查用户是否存在
        if (this.getById(user.getId()) == null) {
            throw new GlobalException("用户不存在");
        }
        
        // 如果更新用户名，检查新用户名是否已被其他用户使用
        if (user.getUsername() != null) {
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUsername, user.getUsername());
            queryWrapper.ne(User::getId, user.getId());
            if (this.count(queryWrapper) > 0) {
                throw new GlobalException("用户名已被使用");
            }
        }
        
        // 更新时间
        user.setUpdateTime(LocalDateTime.now());
        
        return this.updateById(user);
    }

    @Override
    public boolean deleteUser(Integer id) {
        // 检查用户是否存在
        if (this.getById(id) == null) {
            throw new GlobalException("用户不存在");
        }
        
        return this.removeById(id);
    }
}
